import java.util.HashSet;

public class work1 {
    public static int totalFruit(int[] fruits) {
        int left=0,right=0;
        HashSet<Integer>set=new HashSet<>();
        int position=-1;
        int max=0;
        int i=0;
        int j=0;
        int position2=-1;
        boolean flg=true;
        int []arr1=new int[10^5];
        while(right<fruits.length){
            if(!set.contains(fruits[right])){
                arr1[i]=fruits[right];
                i++;
            }
            set.add(fruits[right]);
            if(set.size()>2){
                left=position;
                set.remove(arr1[j]);
                j++;
                left++;
                position=left;
                 flg=true;
                 if(flg){
                     left=position2;
                     set.remove(arr1[j]);
                     j++;
                     left++;
                     position2=left;
                 }
            }
            if(left<=right&&fruits[right]==arr1[j]){
                position=right;
            }
            if(left<=right&&fruits[right]==arr1[j+1]){
                position2=right;
                flg=false;
            }
            if(set.contains(fruits[right])){
                max=Math.max(max,right-left+1);
            }
            right++;
        }
        return max;
    }

    public static void main(String[] args) {
        int []num={6,2,1,1,3,6,6};
        System.out.println(totalFruit(num));
    }
}
